package com.xkstream.client.ui;

import android.content.Intent;
import android.graphics.SurfaceTexture;
import android.os.Bundle;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.xkstream.client.R;
import com.xkstream.client.base.BaseActivity;
import com.xkstream.client.camera.camera.MediaPlayerManager;
import com.xkstream.client.rx.Rxbus;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;

/**
 * Created by xiaoyi on 2017-10-13.
 */

public class VideoPlayActivity extends BaseActivity {
    @BindView(R.id.play)
    TextureView play;
    String path;
    MediaPlayerManager manager;
    @BindView(R.id.progress)
    ProgressBar bar;
    @BindView(R.id.ivBack)
    ImageView ivBack;
    @BindView(R.id.ivPause)
    ImageView ivPause;

    private CompositeDisposable disposable;

    @Override
    protected void initViews(Bundle savedInstanceState) {
        setContentView(R.layout.activity_videoplay);
        ButterKnife.bind(this);
        path = getIntent().getStringExtra("path");
        manager = MediaPlayerManager.getInstance(getApplication());

        startPlay();

        play.setOnClickListener((v) -> {
            manager.pauseMedia();
        });

        ivBack.setOnClickListener((v)->{
            finish();
        });

        disposable.add(Rxbus.getDefault().register(String.class)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe((str) -> {
                    if (str.equals("play")) {
                        //showToast("play");
                        bar.setVisibility(View.GONE);
                    } else if (str.equals("pause")) {
                        ivBack.setVisibility(View.VISIBLE);
                        ivPause.setVisibility(View.VISIBLE);
                    } else if (str.equals("start")) {
                        ivBack.setVisibility(View.GONE);
                        ivPause.setVisibility(View.GONE);
                    }
                }));

    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    private void startPlay() {
        play.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
                manager.playMedia(new Surface(play.getSurfaceTexture()), path);
            }

            @Override
            public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {

            }

            @Override
            public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
                return false;
            }

            @Override
            public void onSurfaceTextureUpdated(SurfaceTexture surface) {

            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        manager.stopMedia();
        disposable.clear();
    }

}


